package com.bestcem.xm.qdes.mq.send.biz.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.config.mq.XmMessageProducer;
import com.bestcem.xm.common.core.constant.StreamMqBinderConstants;
import com.bestcem.xm.qdes.mq.dto.CreateProjectDto;
import com.bestcem.xm.qdes.mq.dto.DeleteProjectDto;
import com.bestcem.xm.qdes.mq.dto.ProjectPermissionChangedDto;
import com.bestcem.xm.qdes.mq.dto.StatusDto;
import com.bestcem.xm.qdes.mq.send.biz.QdesMessageSendService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class QdesMessageSendServiceImpl implements QdesMessageSendService {

    private final XmMessageProducer messageProducer;

    public QdesMessageSendServiceImpl(XmMessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }

    @Override
    public void projectPermissionChangedSend(ProjectPermissionChangedDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_PERMISSION_CHANGE, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]权限变更消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void qdesProjectCreatedSend(CreateProjectDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.QDES_PROJECT_CREATED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]创建项目消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void projectStatusChangedSend(StatusDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_STATUS_CHANGED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目状态变更消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void projectUpdatedSend(StatusDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_UPDATED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目变更消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void projectDeletedSend(DeleteProjectDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_DELETED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目删除消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void projectDeletedShareSend(JSONObject dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_DELETED_SHARE, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目删除分享消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void projectFirstReleasedSend(JSONObject dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_FIRST_RELEASED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目首次发布消息:{}", dto.toJSONString(), e);
        }
    }

    @Override
    public void projectReleasedSend(JSONObject dto) {
        try {
            log.error("qdes.project_released 消息体：" + dto.toJSONString());
            messageProducer.send(StreamMqBinderConstants.PROJECT_RELEASED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目发布消息:{}", dto.toJSONString(), e);
        }
    }

    @Override
    public void projectQuestionsChangedSend(JSONObject dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_QUESTIONS_CHANGED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目题目变化发布消息:{}", dto.toJSONString(), e);
        }
    }
}
